package com.example.backend.controller.login;

import com.example.backend.component.JwtTokenUtil;
import com.example.backend.dto.UserLogin;
import com.example.backend.security.SecurityUser;
import com.example.backend.security.custom.CustomAuthenticationToken;
import com.example.backend.utils.ResultJson;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/login")
@Tag(name = "登录接口")
public class LoginController {

    private final AuthenticationManager authenticationManager;
    private final JwtTokenUtil jwtTokenUtil;

    private HashMap<String, String> checkLogin(Authentication authentication) {
        SecurityUser principal = (SecurityUser) authentication.getPrincipal();
        HashMap<String, Object> map = new HashMap<>(2);
        map.put("userId", principal.getUserId());
        map.put("permissions", principal.getAuthorities());
        String jwt = jwtTokenUtil.generateAccessToken(principal, map);
        map.remove("permissions");
        String refreshToken = jwtTokenUtil.generateRefreshToken(principal, map);
        HashMap<String, String> token = new HashMap<>(2);
        token.put("accessToken", jwt);
        token.put("refreshToken", refreshToken);
        return token;
    }

    @Operation(summary = "密码登录", description = "用户名密码登录", tags = {"登录接口"})
    @PostMapping("/password")
    public ResultJson<?> login(String username, String password) {
        UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken =
                new UsernamePasswordAuthenticationToken(username, password);
        Authentication authentication = authenticationManager.authenticate(usernamePasswordAuthenticationToken);
        HashMap<String, String> token = checkLogin(authentication);
        return ResultJson.success(token);
    }

    @Operation(summary = "JWT登录", description = "JWT登录", tags = {"登录接口"})
    @PostMapping("/jwt")
    public ResultJson<?> password(@RequestBody UserLogin userLogin) {
        CustomAuthenticationToken customAuthenticationToken = new CustomAuthenticationToken(userLogin.getUsername(), userLogin.getPassword());
        Authentication authentication = authenticationManager.authenticate(customAuthenticationToken);
        HashMap<String, String> token = checkLogin(authentication);
        return ResultJson.success(token);
    }

    @Operation(summary = "刷新JWT", description = "刷新JWT", tags = {"登录接口"})
    @PostMapping("/refresh")
    public ResultJson<?> refresh(@RequestBody HashMap<String, String> map) {
        // Todo: 2023/9/23 刷新token逻辑
        String refreshToken = map.get("refreshToken");
        String accessToken = jwtTokenUtil.refreshAccessTokenWithRefreshToken(refreshToken);
        HashMap<String, String> token = new HashMap<>(1);
        token.put("accessToken", accessToken);
        return ResultJson.success(token);
    }

}
